package com.example.hotelmanager.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.hotelmanager.entity.Front;
import com.example.hotelmanager.entity.vo.Response;
import com.example.hotelmanager.entity.vo.ResponseMsg;
import com.example.hotelmanager.service.BackgroundService;
import com.example.hotelmanager.service.FrontService;
import com.example.hotelmanager.utils.JwtUtill;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author :chenjie
 * @date :Created 2022/11/28 20:27
 */
@CrossOrigin
@RestController
@Slf4j
public class FrontController {
    @Autowired
    private FrontService frontService;
    @Autowired
    private JwtUtill jwtUtill;
    @Autowired
    private BackgroundService backgroundService;
    @PostMapping("/frontLogin")
    public Response Login(String frontId,String password){
        LambdaQueryWrapper<Front> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.eq(Front::getFrontId,frontId);
        queryWrapper.eq(Front::getPassword,password);
        Front one = frontService.getOne(queryWrapper);
        if(one!=null){
            String token = jwtUtill.updateJwt(frontId);
            return (new Response()).success(token);
        }
        return new Response(ResponseMsg.NO_SUCH_USER);
    }
    @PostMapping("/addFront")
    public Response addFront(HttpServletRequest request, Front front){
        String num = (String) request.getAttribute("num");
        if(backgroundService.getById(num) != null){
            frontService.saveOrUpdate(front);
            String token = jwtUtill.updateJwt(num);
            return (new Response()).success(token);
        }
        return new Response(ResponseMsg.ILLEGAL_OPERATION);
    }

    @DeleteMapping("/deleteFront")
    public Response deleteFront(HttpServletRequest request,int id){
        String num = (String) request.getAttribute("num");
        if(backgroundService.getById(num) != null){
            frontService.removeById(id);
            String token = jwtUtill.updateJwt(num);
            return (new Response()).success(token);
        }
        return new Response(ResponseMsg.ILLEGAL_OPERATION);
    }

    @GetMapping("/getAllFront")
    public Response getAllFront(HttpServletRequest request){
        String num = (String) request.getAttribute("num");
        if(backgroundService.getById(num) != null){
            List<Front> fronts = frontService.list();
            if(fronts != null && fronts.size()>0){
                Map<String,Object> resultMap = new HashMap<>();
                String token = jwtUtill.updateJwt(num);
                resultMap.put("fronts",fronts);
                resultMap.put("token",token);
                return (new Response()).success(resultMap);
            }
            return new Response(ResponseMsg.NO_TARGET);
        }
        return new Response(ResponseMsg.ILLEGAL_OPERATION);
    }

    @GetMapping("/getFrontById")
    public Response getFrontById(HttpServletRequest request,String frontId){
        String num = (String) request.getAttribute("num");
        if(backgroundService.getById(num) != null){
            Front front = frontService.getById(frontId);
            if(front != null ){
                Map<String,Object> resultMap = new HashMap<>();
                String token = jwtUtill.updateJwt(num);
                resultMap.put("front",front);
                resultMap.put("token",token);
                return (new Response()).success(resultMap);
            }
            return new Response(ResponseMsg.NO_TARGET);
        }
        return new Response(ResponseMsg.ILLEGAL_OPERATION);
    }
}
